package Common;


//import FilePack;

//import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

//import coreLG.GameScr;

public class CFont {
	public static final int LEFT = 0;
	public static final int RIGHT = 1;
	public static final int CENTER = 2;

	private Image imgFont;
	private final String charList;
	public final byte[] charWidth;
	private int charHeight;
	private int charSpace;
	public int imgWidth;
        
	public static CFont normalFont = new CFont(
			" 0123456789.,:!?()+*$#/-%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZĐ~",//
			new byte[] { 4, 6, 5, 6, 6, 7, 6, 6, 6, 6, 6, 3, 3, 3, 4, 5, 4, 4,
					6, 5, 8, 8, 6, 6, 10, 6, 7, 5, 7, 6, 4, 7, 7, 3, 4, 6, 3,
					9, 7, 7, 7, 7, 5, 5, 4, 7, 6, 9, 6, 7, 6, 6, 6, 6, 6, 6, 6,
					6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
					6, 6, 3, 3, 3, 5, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
					7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7, 7, 8,
					7, 7, 7, 7, 7, 6, 6, 7, 7, 3, 5, 7, 6, 10, 8, 7, 7, 7, 6,
					7, 7, 7, 7, 9, 7, 7, 8, 8, 6 },//
			13, "fb.png", 0);

	public static CFont borderFont = new CFont(
			" 0123456789.,:!?()+*$#/-%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZĐ~",//
			new byte[] { 4, 6, 5, 6, 6, 7, 6, 6, 6, 6, 6, 3, 3, 3, 4, 5, 4, 4,
					6, 5, 8, 8, 6, 6, 10, 6, 7, 5, 7, 6, 4, 7, 7, 3, 4, 6, 3,
					9, 7, 7, 7, 7, 5, 5, 4, 7, 6, 9, 6, 7, 6, 6, 6, 6, 6, 6, 6,
					6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
					6, 6, 3, 3, 3, 5, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
					7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7, 7, 8,
					7, 7, 7, 7, 7, 6, 6, 7, 7, 3, 5, 7, 6, 10, 8, 7, 7, 7, 6,
					7, 7, 7, 7, 9, 7, 7, 8, 8, 6 },//
			15, "fb1.png", 0);
	
	public static CFont borderFont_ye = new CFont(
			" 0123456789.,:!?()+*$#/-%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZĐ~",//
			new byte[] { 4, 6, 5, 6, 6, 7, 6, 6, 6, 6, 6, 3, 3, 3, 4, 5, 4, 4,
					6, 5, 8, 8, 6, 6, 10, 6, 7, 5, 7, 6, 4, 7, 7, 3, 4, 6, 3,
					9, 7, 7, 7, 7, 5, 5, 4, 7, 6, 9, 6, 7, 6, 6, 6, 6, 6, 6, 6,
					6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
					6, 6, 3, 3, 3, 5, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
					7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 7, 7, 7, 7, 8,
					7, 7, 7, 7, 7, 6, 6, 7, 7, 3, 5, 7, 6, 10, 8, 7, 7, 7, 6,
					7, 7, 7, 7, 9, 7, 7, 8, 8, 6 },//
			15, "fb1_ye.png", 0);
	

	public static CFont bigFont = new CFont(
			" 0123456789.,:!?()-'/ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ",//
			new byte[] { 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8, 6, 6,
					6, 3, 7, 10, 10, 10, 10, 8, 8, 10, 10, 5, 8, 9, 8, 13, 11,
					10, 10, 10, 10, 10, 9, 10, 10, 13, 11, 11, 9, 10, 10, 10,
					10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 8,
					8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 5, 5, 5, 5, 5, 10, 10, 10,
					10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
					10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
					10 },//
			21, "fcg14m.png", 0);
	public static CFont bigFont_ye = new CFont(
			" 0123456789.,:!?()-'/ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ",//
			new byte[] { 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8, 6, 6,
					6, 3, 7, 10, 10, 10, 10, 8, 8, 10, 10, 5, 8, 9, 8, 13, 11,
					10, 10, 10, 10, 10, 9, 10, 10, 13, 11, 11, 9, 10, 10, 10,
					10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 8,
					8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 5, 5, 5, 5, 5, 10, 10, 10,
					10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
					10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
					10 },//
			21, "fcg14m_ye.png", 0);
	public static CFont bigFont_xam = new CFont(
			" 0123456789.,:!?()-'/ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ",//
			new byte[] { 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8, 6, 6,
					6, 3, 7, 10, 10, 10, 10, 8, 8, 10, 10, 5, 8, 9, 8, 13, 11,
					10, 10, 10, 10, 10, 9, 10, 10, 13, 11, 11, 9, 10, 10, 10,
					10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 8,
					8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 5, 5, 5, 5, 5, 10, 10, 10,
					10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
					10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
					10 },//
			21, "fcg14m_green.png", 0);


	public static CFont numberFont = new CFont("0123456789$+-",//
			new byte[] { 15, 12, 15, 15, 15, 15, 15, 15, 15, 15, 15, 10, 10 },//
			13, "so.png", -3);

	public static CFont smallFontRed = new CFont(
			"0123456789+-%$:abcdefghijklmnopqrstuvwxyz",//
			new byte[] { 5, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 5, 3, 5, 5, 5,
					5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 6, 5, 5, 5, 5, 5, 5, 5, 5, 7,
					5, 5, 5 },//
			8, "fs0.png", -1);

	public static CFont smallFontYellow = new CFont(
			"0123456789+-%$:abcdefghijklmnopqrstuvwxyz",//
			new byte[] { 5, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 5, 3, 5, 5, 5,
					5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 6, 5, 5, 5, 5, 5, 5, 5, 5, 7,
					5, 5, 5 },//
			8, "fs1.png", -1);
	public static CFont smallFont = new CFont(
			" 0123456789.,:!?()+*$#/-%abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",//
			new byte[] { 2, 5, 4, 5, 5, 6, 5, 5, 5, 5, 5, 2, 3, 2, 2, 5, 4, 4,
					6, 6, 6, 6, 4, 3, 8, 5, 5, 4, 5, 5, 4, 5, 5, 2, 3, 5, 2, 8,
					5, 5, 5, 5, 4, 4, 3, 5, 6, 8, 4, 4, 4, 6, 5, 6, 6, 5, 5, 6,
					6, 4, 4, 5, 5, 8, 7, 7, 5, 7, 6, 6, 6, 6, 6, 8, 6, 6, 5, },//
			10, "tahoma9.png", 0);

	public static CFont arialFont = new CFont(
			" 0123456789.,:!?()+*$#/-%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZĐ~",
			new byte[] { 3, 5, 3, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 5, 3, 2,
					5, 4, 5, 6, 2, 2, 9, 5, 5, 4, 5, 5, 3, 5, 5, 1, 1, 5, 1, 9,
					5, 5, 5, 5, 3, 5, 2, 5, 4, 8, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
					5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
					5, 2, 1, 2, 3, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
					6, 6, 6, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 4, 4, 4, 4, 4, 6,
					6, 6, 7, 7, 6, 5, 7, 7, 1, 4, 7, 6, 7, 7, 7, 6, 7, 7, 6, 6,
					7, 6, 8, 6, 6, 6, 7, 4, }, 14, "arialf.png", 1);

	public static CFont blackF = new CFont(
			" 0123456789.,:!?()+*$#/-%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZĐ~Á",//
			new byte[] { 4, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 2, 2, 6, 4, 4,
					6, 5, 6, 7, 4, 4, 10, 6, 6, 6, 6, 6, 4, 6, 6, 2, 2, 5, 2,
					8, 6, 6, 6, 6, 4, 6, 3, 6, 6, 10, 6, 6, 6, 6, 6, 6, 6, 6,
					6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
					6, 6, 6, 3, 2, 3, 4, 2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
					7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6,
					6, 7, 8, 7, 7, 7, 6, 6, 8, 7, 2, 5, 7, 6, 8, 7, 8, 6, 8, 7,
					7, 6, 7, 8, 11, 7, 8, 7, 7, 6, 7 },//
			13, "arial11b.png", 0);


	public CFont(String charList, byte[] charWidth, int charHeight,
			String fontFile, int charSpace) {
		//FilePack filePak;
		//filePak = new FilePack(GameScr.PATH_FONT + "font");   
		this.charSpace = charSpace;
		this.charList = charList;
		this.charWidth = charWidth;
		this.charHeight = charHeight;
		try {
			//imgFont = filePak.loadImage(fontFile);
//                        imgFont = CRes.loadImage("/font/"+fontFile);//(fileFontImgName);
			imgFont=Image.createImage("/font/"+fontFile);
			imgWidth = imgFont.getWidth();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//filePak = null;
	}
	
	public void drawString(Graphics g, String st, int x, int y, int align) {                
		int pos;
		int x1;
		int len = st.length();			
		
		if (align == 0)
			x1 = x;
		else if (align == 1)
			x1 = x - getWidth(st);
		else
			x1 = x - (getWidth(st) >> 1);
		
		for (int i = 0; i < len; i++) {
			pos = charList.indexOf(st.charAt(i));
			if (pos == -1)
				pos = 0;
			if (pos > -1)
				g.drawRegion(imgFont, 0, pos * charHeight, imgWidth,
						charHeight, 0, x1, y, 20);
			x1 += (charWidth[pos] + charSpace);
		}
	}
	
	public static String replace(String _text, String _searchStr, String _replacementStr) {
		// String buffer to store str
		StringBuffer sb = new StringBuffer();

		// Search for search
		int searchStringPos = _text.indexOf(_searchStr);
		int startPos = 0;
		int searchStringLength = _searchStr.length();

		// Iterate to add string
		while (searchStringPos != -1) {
			sb.append(_text.substring(startPos, searchStringPos)).append(_replacementStr);
			startPos = searchStringPos + searchStringLength;
			searchStringPos = _text.indexOf(_searchStr, startPos);
		}

		// Create string
		sb.append(_text.substring(startPos, _text.length()));

		return sb.toString();
	}	
	public int getWidth(String st) {
		int pos;
		int len = 0;
		for (int i = 0; i < st.length(); i++) {
			pos = charList.indexOf(st.charAt(i));
			if (pos == -1)
				pos = 0;
			len += (charWidth[pos]);
		}
		return len;
	}

	public String[] splitFontBStrInLine(String src, int lineWidth) {
		Vector list = new Vector();
		int srclen = src.length();
		if (srclen <= 1)
			return new String[] { src };
		String tem = "";
		int start = 0, end = 0;
		while (true) {
			while (getWidth(tem) < lineWidth) {
				tem += src.charAt(end);
				end++;
				if (src.charAt(end) == '\n')
					break;
				if (end >= srclen - 1) {
					end = srclen - 1;
					break;
				}
			}
			if (end != srclen - 1 && (src.charAt(end + 1) != ' ')) {
				int endAnyway = end;
				while (true) {
					if (src.charAt(end + 1) == '\n')
						break;
					if (src.charAt(end + 1) == ' ' && src.charAt(end) != ' ')
						break;
					if (end == start)
						break;
					end--;
				}
				if (end == start)
					end = endAnyway;
			}
			list.addElement(src.substring(start, end + 1));
			if (end == srclen - 1)
				break;
			start = end + 1;
			while (start != srclen - 1 && src.charAt(start) == ' ')
				start++;
			if (start == srclen - 1)
				break;
			end = start;
			tem = "";
		}
		String[] strs = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			strs[i] = (String) list.elementAt(i);
		}
		return strs;
	}

	public int getHeight() {
		return charHeight;
	}
}
